package com.sjd.Controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class JDBCController {
    @Autowired
    JdbcTemplate jdbcTemplate;


    //查询数据库的所以信息
    @GetMapping("/userlist")
    public List<Map<String,Object>>userlist(){
        String sql="select * from mybatis.user";
        List<Map<String, Object>> list_maps = jdbcTemplate.queryForList(sql);
        return list_maps;
    }

    //增加一个用户
    @GetMapping("/adduser")
    public String add(){
        String sql="insert into mybatis.user(id,name,pwd) values(4,'小东','123')";
        jdbcTemplate.update(sql);
        return "add ok";
    }
    //增加一个用户
    @GetMapping("/updateuser/{id}")
    public String update(@PathVariable("id") int id){
        String sql="update mybatis.user set name=?,pwd=?  where id=" +id;
        Object[] objects = new Object[2];
        objects[0]="小东2";
        objects[1]="111";
        jdbcTemplate.update(sql,objects);
        return "update ok";
    }

    //删除用户
    @GetMapping("/delete/{id}")
    public String deleteuser(@PathVariable("id") int id){
        String sql="delete from mybatis.user where id=?";
        jdbcTemplate.update(sql,id);
        return "delete_ok";
    }
}
